<?php

namespace app\admin\validate;

use app\admin\model\SystemAdmin as ModelSystemAdmin;
use W7\Validate\Validate;

class SystemAdmin extends Validate
{
    protected $rule = [
        'role_id' => 'required',
        'username' => 'required|verifyAdd',
        'password' => 'required',
        'nickname' => 'required',
        'headimg' => 'required',
    ];

    protected $message = [
        'role_id.required' => '请选择所属部门',
        'username.required' => '请输入登录账号',
        'password.required' => '请输入登录密码',
        'nickname.required' => '请输入用户昵称',
        'headimg.required' => '请上传头像',
    ];

    /**
     * 登录场景
     * @return Validate
     * @copyright 贵州猿创科技有限公司
     * @Email 416716328@qq.com
     * @DateTime 2023-04-30
     */
    protected function sceneLogin($scene)
    {
        return $scene
            ->only([
                'username',
                'password',
            ])
            ->remove('username', ['verifyAdd']);
    }

    /**
     * 添加场景验证
     * @return Validate
     * @copyright 贵州猿创科技有限公司
     * @Email 416716328@qq.com
     * @DateTime 2023-04-30
     */
    protected function sceneAdd($scene)
    {
        return $scene
            ->only([
                'role_id',
                'username',
                'password',
                'nickname',
                'headimg',
            ]);
    }

    /**
     * 修改场景
     * @return Validate
     * @copyright 贵州猿创科技有限公司
     * @Email 416716328@qq.com
     * @DateTime 2023-04-30
     */
    protected function sceneEdit($scene)
    {
        return $scene
            ->only([
                'role_id',
                'username',
                'nickname',
                'headimg',
            ])
            ->remove('username', ['verifyAdd']);
    }

    /**
     * 修改自身数据
     * @return Validate
     * @copyright 贵州猿创科技有限公司
     * @Email 416716328@qq.com
     * @DateTime 2023-04-30
     */
    protected function sceneEditSelf($scene)
    {
        return $scene
            ->only([
                'username',
                'nickname',
                'headimg'
            ])
            ->remove('role_id')
            ->remove('username', ['verifyAdd']);
    }

    /**
     * 验证是否存在
     * @param mixed $value
     * @return bool|string
     * @copyright 贵州猿创科技有限公司
     * @Email 416716328@qq.com
     * @DateTime 2023-04-30
     */
    protected function ruleVerifyAdd($field, $value, $parameters, $validator)
    {
        $where = [
            'username' => $value
        ];
        if (ModelSystemAdmin::where($where)->count()) {
            return '该用户已存在';
        }
        return true;
    }
}